﻿using QuanLyTiemVang.BUS;
using QuanLyTiemVang.Classes;
using QuanLyTiemVang.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace QuanLyTiemVang.GUI
{
    /// <summary>
    /// Interaction logic for BaoCaoDoanhThu.xaml
    /// </summary>
    public partial class BaoCaoDoanhThu : Window, IPhanQuyenHelper
    {

        public BaoCaoDoanhThu()
        {
            InitializeComponent();
            dpNgayBaoCao.SelectedDate = DateTime.Now.Date;
            this.Loaded += BaoCaoDoanhThu_Loaded;
        }

        void BaoCaoDoanhThu_Loaded(object sender, RoutedEventArgs e)
        {
            CurrentUser.Instance.AddForm(this);
        }
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);

            // Begin dragging the window
            this.DragMove();
        }
        private void btnLapBaoCao_Click(object sender, RoutedEventArgs e)
        {
            List<PHIEUBANHANG> listPhieuBanHang = PhieuBanHangDAO.SelectPhieuBanHangByNgayThanhToan(new PHIEUBANHANG { NgayThanhToan = dpNgayBaoCao.SelectedDate });
            dgPhieuBanHang.ItemsSource = listPhieuBanHang;
            List<PHIEUDICHVU> listPhieuDichVu = PhieuDichVuDAO.SelectPhieuDichVuByNgayGiao(new PHIEUDICHVU { NgayGiao = dpNgayBaoCao.SelectedDate });
            dgPhieuDichVu.ItemsSource = listPhieuDichVu;
            List<PHIEUMUAHANG> listPhieuMuaHang = PhieuMuaHangDAO.SelectPhieuMuaHangByNgayThanhToan(new PHIEUMUAHANG { NgayThanhToan = dpNgayBaoCao.SelectedDate });
            dgDanhSachPhieuMuaHang.ItemsSource = listPhieuMuaHang;
            List<PHIEUHANGGIACONG> listPhieuHangGiaCong = PhieuHangGiaCongDAO.SelectPhieuHangGiaCongByNgayThanhToan(new PHIEUHANGGIACONG { NgayThanhToan = dpNgayBaoCao.SelectedDate });
            dgDanhSachPhieuGiaCong.ItemsSource = listPhieuHangGiaCong;
            List<PHIEUCHI> listPhieuChi = PhieuChiDAO.SelectPhieuChiByNgayChi(new PHIEUCHI { NgayChi = dpNgayBaoCao.SelectedDate });
            dgDanhSachPhieuChi.ItemsSource = listPhieuChi;

            decimal tienPBH = 0;
            if (listPhieuBanHang != null)
            {
                foreach (var item in listPhieuBanHang)
                {
                    if (item.TongCong.HasValue)
                    {
                        tienPBH += item.TongCong.Value;
                    }
                }
            }
            txtTienThuPhieuBanHang.Text = tienPBH.ToString();

            decimal tienPDV = 0;
            if (listPhieuDichVu != null)
            {
                foreach (var item in listPhieuDichVu)
                {
                    if (item.TongCong.HasValue)
                    {
                        tienPDV += item.TongCong.Value;
                    }
                }
            }
            txtTienPhieuDichVu.Text = tienPDV.ToString();

            decimal tienPMH = 0;
            if (listPhieuMuaHang != null)
            {
                foreach (var item in listPhieuMuaHang)
                {
                    if (item.TongCong.HasValue)
                    {
                        tienPMH += item.TongCong.Value;
                    }
                }
            }
            txtTienPhieuChiMuaHang.Text = tienPMH.ToString();

            decimal tienPHGC = 0;
            if (listPhieuHangGiaCong != null)
            {
                foreach (var item in listPhieuHangGiaCong)
                {
                    if (item.TongCong.HasValue)
                    {
                        tienPMH += item.TongCong.Value;
                    }
                }
            }
            txtTienChiPhieuHangGiaCong.Text = tienPHGC.ToString();

            decimal tienPC = 0;
            if (listPhieuChi != null)
            {
                foreach (var item in listPhieuChi)
                {
                    if (item.TongCong.HasValue)
                    {
                        tienPMH += item.TongCong.Value;
                    }
                }
            }
            txtTienChiPhieuChi.Text = tienPC.ToString();

            decimal tongTienThu = tienPBH + tienPDV;
            txtTongTienThu.Text = tongTienThu.ToString();
            decimal tongTienChi = tienPMH + tienPHGC + tienPC;
            txtTongTienChi.Text = tongTienChi.ToString();

            BAOCAODOANHTHU lastBcdt = BaoCaoDoanhThuDAO.SelectLast();
            decimal tonQuyBanDau, tonQuyConLai;
            tonQuyBanDau = 0;
            tonQuyConLai = 0;
            if (lastBcdt != null)
            {
                tonQuyBanDau = Convert.ToInt32(lastBcdt.TonQuyConLai);
            }
            tonQuyConLai = tonQuyBanDau + tongTienThu - tongTienChi;
            txtTonQuyBanDau.Text = tonQuyBanDau.ToString();
            txtTonQuyConLai.Text = tonQuyConLai.ToString();

            DANHSACHKHOANTHU dskt = new DANHSACHKHOANTHU();
            dskt.NgayThu = dpNgayBaoCao.SelectedDate;
            dskt.TienThuPhieuBanHang = (int)tienPBH;
            dskt.TienThuPhieuDichVu = (int)tienPDV;
            dskt.TongTienThu = tongTienThu;
            DanhSachKhoanThuBUS.ThemDanhSachKhoanThu(dskt);

            DANHSACHKHOANCHI dskc = new DANHSACHKHOANCHI();
            dskc.NgayChi = dpNgayBaoCao.SelectedDate;
            dskc.TienChiPhieuMuaHang = tienPMH;
            dskc.TienChiPhieuHangGiaCong = tienPHGC;
            dskc.TienChiPhieuChi = tienPC;
            dskc.TongTienChi = tongTienChi;
            DanhSachKhoanChiBUS.ThemDanhSachKhoanChi(dskc);

            BAOCAODOANHTHU bcdt = new BAOCAODOANHTHU();
            bcdt.NgayBaocao = dpNgayBaoCao.SelectedDate;
            bcdt.TonQuyBanDau = tonQuyBanDau;
            bcdt.TonQuyConLai = tonQuyConLai;
            BaoCaoDoanhThuBUS.ThemBaoCaoDoanhThu(bcdt);
            txtMaBaoCao.Text = bcdt.MaBaoCaoDoanhThu.ToString();

            MessageBox.Show("Lập báo cáo thành công!");
            this.btnLapBaoCao.IsEnabled = false;
        }

        private void btnThoat_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void dpNgayBaoCao_SelectedDateChanged_1(object sender, SelectionChangedEventArgs e)
        {
            DatePicker dp = sender as DatePicker;
            if (dp.SelectedDate.HasValue)
            {
                DateTime dt = dp.SelectedDate.Value;
                if (dt < DateTime.Now.Date)
                    dp.SelectedDate = DateTime.Now.Date;
            }
        }

        void IPhanQuyenHelper.SetAdminFunction()
        {
        }

        void IPhanQuyenHelper.SetStaffFunction()
        {
        }

        void IPhanQuyenHelper.SetCustomerFunction()
        {
        }

        void IPhanQuyenHelper.SetNonFunction()
        {
            this.Close();
        }
    }
}
